/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dnbW.dbW;

import java.util.ArrayList;
import java.util.Collection;
import javax.swing.tree.DefaultMutableTreeNode;

/**
 * Encapsulates a data source
 * @author Truong Ba Quan
 */
public class DataSource {
    public static final String EMPTY_DATASOURCE_INFO = "Empty Data Source";

    // <editor-fold defaultstate="collapsed" desc="Name">
    private String name;

    public String getName() {
        return name;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Database">
    private DatabaseInfo database;

    public DatabaseInfo getDatabase() {
        return this.database;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Tree Node">
    private DefaultMutableTreeNode treeNode;

    public DefaultMutableTreeNode getTreeNode() {
        return treeNode;
    }

    public void setTreeNode(DefaultMutableTreeNode treeNode) {
        this.treeNode = treeNode;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Documents">
    private ArrayList<Document> documents;

    /**
     * Checks whether a document with the given Id exists
     * @param docId
     * @return
     */
    public boolean hasDocument(int docId) {
        if (docId < 1) return false;
        if (this.documents.size() < docId) return false;
        return (this.documents.get(docId-1) != null);
    }

    /**
     * Try to get a document with given docId.
     * If it exists, retrieve it; if not, create a new one
     * @param docId
     * @return
     */
    public Document tryGetDocument(int docId) {
        if (this.hasDocument(docId)) {
            return this.documents.get(docId-1);
        }
        else {
            Document newDocument = new Document(this, docId);
            this.addDocument(newDocument);
            return newDocument;
        }
    }

    /**
     * Gets a document with the given id. If a document with that Id does not exist,
     * null is returned.
     * @param docId
     * @return
     */
    public Document getDocument(int docId) {
        if (this.hasDocument(docId)) {
            return this.documents.get(docId-1);
        }
        else return null;
    }

    public void addDocument(Document document) {
        this.documents.ensureCapacity(document.getDocId());
        this.documents.add(document.getDocId()-1, document);
    }

    public Collection<Document> getDocuments() {
        return this.documents;
    }

    public int getNumDocuments() {
        return this.documents.size();
    }

    public void clearDocuments() {
        this.documents.clear();
    }
    // </editor-fold>

    public DataSource(DatabaseInfo database, String name) {
        this.database = database;
        this.name = name;
        this.documents = new ArrayList<Document>();
        this.database.getEngine().retrieveDocuments(this);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final DataSource other = (DataSource) obj;
        if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 97 * hash + (this.name != null ? this.name.hashCode() : 0);
        return hash;
    }

    @Override
    public String toString() {
        return this.getName();
    }
}
